/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package historyBean;

import java.rmi.RemoteException;
import java.sql.*;
import javax.ejb.*;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Session Bean for work with history
 * @author Konstantin
 */
public class HistoryBean implements SessionBean {
    
    private SessionContext sessionContext;
    private DataSource dataSource;
    private Connection connection;
    
    public void setSessionContext(SessionContext ctx) {
        
        sessionContext = ctx;
    }

    public void ejbRemove() {
        
    }

    public void ejbActivate() {
        
    }

    public void ejbPassivate() {
        
    }
    
    public void ejbCreate() {
        
    }
    
    void setConnection(){ 
        
        InitialContext initialContext = null;
        
        try {
            initialContext = new InitialContext();
            dataSource = (DataSource) initialContext.lookup(
                        "java:/ATeamConnectionPool");
            System.out.println("dataSource is found");                
        } catch (NamingException ex){
            
            throw new EJBException(ex);
        }
        if (connection == null){
            
            try {
                
                connection = dataSource.getConnection();
                System.out.println("Connection is ready");
            } catch (SQLException ex){
                
                throw new EJBException(ex);
            }
        }    
    }
    
    void closeConnection() {
        
        try {
            
            if (connection != null){
                connection.close();
                System.out.println("Connection closed");
            }
        } catch (SQLException ex) {
            
            throw new EJBException(ex);
        } finally {
            connection = null;
        }
    }
    
    /**
     * Saves History message in database
     * @param mapMessage Message with objectId, ActionId and 
     * Description
     */
    public void saveHistory(MapMessage mapMessage) {
        
       String saveHistory =
               "INSERT INTO History "
               + "VALUES (?,?,?,TO_CHAR(SYSDATE,'DD.MM.YYYY HH24:MI:SS'))";
       PreparedStatement saveHistoryStat = null;
       
       try{
           
           setConnection();
           
           saveHistoryStat = 
                   connection.prepareStatement(saveHistory);
           
           saveHistoryStat.setInt(1, mapMessage.getInt("ObjectId"));
           saveHistoryStat.setInt(2, mapMessage.getInt("ActionId"));
           saveHistoryStat.setString(3, mapMessage.getString("Description"));
           
           saveHistoryStat.executeUpdate();
       }
       catch(SQLException ex){
           
           throw new EJBException(ex);
       }
       catch(JMSException ex){
           
           throw new EJBException(ex);
       }
       finally {
           
            closeConnection();
       }
    }
}